using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using System.Collections.Specialized;
using AjGenesisStudio.Properties;

namespace AjGenesisStudio.Syntax
{
    class SyntaxHelper
    {
        public static void FillCombo(System.Windows.Forms.ToolStripComboBox cbo)
        {
            XmlReader reader = new XmlTextReader(Configs.GetConfigFile());
            XmlDocument doc = new XmlDocument();
            XmlNodeList nodes;
            XmlElement el;
            Syntax syn;

            doc.Load(reader);
            reader.Close();

            cbo.Items.Clear();

            nodes = doc.SelectNodes("//filetypes/filetype");
            foreach (XmlNode n in nodes)
            {
                if (n != null)
                {
                    el = (XmlElement)n;
                    syn = new Syntax();
                    syn.Name = el.Attributes["name"].Value;
                    syn.Extension = el.Attributes["extension"].Value;
                    syn.Path = SyntaxHelper.GetSynsFolder() + el.Attributes["syntaxfile"].Value;
                    cbo.Items.Add(syn);
                }
            }

            cbo.Sorted = true;
        }

        public static string GetSynsFolder()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "Syns\\";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            return path;
        }

        public static string GetSyntaxFile(string extension)
        {
            XmlDocument doc = Configs.GetConfigDocument();
            XmlNode node;
            string ext = extension;

            if (ext.StartsWith("."))
                ext = ext.Remove(0, 1);

            node = doc.SelectSingleNode("//filetypes/filetype[@extension='" + ext + "']/@syntaxfile");

            if (node == null)
                return string.Empty;
            else
                return GetSynsFolder() + node.Value;
        }

        public static string GetDefaultSyntaxFile()
        {
            return SyntaxHelper.GetSyntaxFile("txt");
        }

        public static Syntax GetSyntaxFromName(string name)
        {
            XmlReader reader = new XmlTextReader(Configs.GetConfigFile());
            XmlDocument doc = new XmlDocument();
            XmlNode node;
            XmlElement el;
            Syntax syn = null;

            doc.Load(reader);
            reader.Close();

            node = doc.SelectSingleNode("//filetypes/filetype[@name='" + name + "']");
            if (node != null)
            {
                el = (XmlElement)node;
                syn = new Syntax();
                syn.Name = el.Attributes["name"].Value;
                syn.Extension = el.Attributes["extension"].Value;
                syn.Path = SyntaxHelper.GetSynsFolder() + el.Attributes["syntaxfile"].Value;
            }
            return syn;
        }

        public static string GetSavedOuputSyntax(string tpl_file)
        {
            if (Settings.Default.OutputSyns == null)
                Settings.Default.OutputSyns = new StringCollection();

            foreach (string i in Settings.Default.OutputSyns)
                if (i.StartsWith(tpl_file + ";"))
                    return i.Substring(i.IndexOf(";") + 1);

            return string.Empty;
        }

        public static void SetSavedOuputSyntax(string tpl_file, string output_syntax)
        {
            if (Settings.Default.OutputSyns == null)
                Settings.Default.OutputSyns = new StringCollection();

            foreach (string i in Settings.Default.OutputSyns)
            {
                if (i.StartsWith(tpl_file + ";")) //ya estaba, lo borro para agregar el actualizado
                {
                    Settings.Default.OutputSyns.Remove(i);
                    break;
                }
            }

            //limito la cantidad a 200 y borro el mas viejo
            if (Settings.Default.OutputSyns.Count > 199)
                Settings.Default.OutputSyns.RemoveAt(0);

            Settings.Default.OutputSyns.Add(tpl_file + ";" + output_syntax);
        }
    }
}
